<template>
  <el-container>
    <!-- // 头部信息 -->
    <el-header>
      <div class="logo-left">
        <h3>
          <span class="title">电商管理系统</span>
        </h3>
        <div class="toggle-button" @click="open">
          <i class="el-icon-s-fold"></i>
        </div>
      </div>

      <div>
        <span @click="loginOut" class="logout">退出</span>
      </div>
    </el-header>
    <!-- 页面主题部分 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <el-menu
          :collapse="isCollapse"
          :collapse-transition="false"
          background-color="#ebeff2"
          :unique-opened="true"
          text-color="#525e76"
          active-text-color="#35383d"
          :default-active="activePath"
          class="el-menu-vertical-demo"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.order+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+sbuItem.path"
              @click="saveLinkStatus('/'+sbuItem.path)"
              v-for="sbuItem in item.children"
              :key="sbuItem.id"
            >
              <i class="el-icon-menu"></i>
              <span>{{sbuItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      iconObj: {
        "125": "el-icon-user-solid",
        "103": "el-icon-s-tools",
        "101": "el-icon-s-goods",
        "102": "el-icon-s-order",
        "145": "el-icon-s-marketing"
      },
      isCollapse: false,
      activePath: ""
    };
  },
  created() {
    this.geMenulist();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    loginOut() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async geMenulist() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      console.log(res);
    },
    open() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存链接激活状态
    saveLinkStatus(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
    }
  }
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
 border-bottom: 1px solid #ebeff2;
  background-color: #35383d;
}
.el-aside {
  color: #ffffff;
  background-color: #ebeff2;
  .el-menu {
    border: none;
  }
}
.el-submenu__title {
  color: #ffffff;
}
.el-main {
  background-color: #ffffff;
}
.toggle-button {
//   background-color: #7890a8;
  font-size: 20px;
  line-height: 20px;
  text-align: center;
  margin-left: 60px;
  color: #ffff;
  cursor: pointer;
}

.title ,.logout{
  color: #fff;
  cursor: pointer;
}
.logo-left {
  align-items: center;
  display: flex;
  justify-content: space-between;
}
</style>